package cn.edu.neusoft.cqy917.zyfypt_cqy917.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import java.util.List;

import cn.edu.neusoft.cqy917.zyfypt_cqy917.R;
import cn.edu.neusoft.cqy917.zyfypt_cqy917.activity.ViewTwareActivity;
import cn.edu.neusoft.cqy917.zyfypt_cqy917.bean.TwareBean;


public class TwareAdapter
        extends RecyclerView
        .Adapter<RecyclerView.ViewHolder> {
    private List<TwareBean> list;
    private Context context;
    public TwareAdapter(Context context) {
        this.context = context;
    }
    public List<TwareBean> getList() {
        return list;
    }
    public void setList(List<TwareBean> list) {
        this.list = list;
        notifyDataSetChanged();
    }
    public class ViewHolder
            extends  RecyclerView.ViewHolder{
        ImageView imageView;
        TextView textView,textView3,textView2;
        Button button;
        public ViewHolder(View itemView) {
            super(itemView);
            imageView = (ImageView) itemView.findViewById(R.id.imageView);
            button = (Button) itemView.findViewById(R.id.button);
            textView = (TextView) itemView.findViewById(R.id.textView);
            textView2 = (TextView) itemView.findViewById(R.id.textView2);
            textView3 = (TextView) itemView.findViewById(R.id.textView3);
        }
    }
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item,parent,false);
        return new ViewHolder(view);
    }
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        TwareBean twareBean = list.get(position);
        if (twareBean == null)
            return;
        ViewHolder viewHolder = (ViewHolder) holder;

        viewHolder.textView.setText(twareBean.getName());
        viewHolder.textView2.setText(twareBean.getDescription());
        viewHolder.textView3.setText(twareBean.getUpdate_time());
        Picasso.with(context).load("http://amicool.neusoft.edu.cn/Uploads/"+twareBean.getThumb()).
                placeholder(R.drawable.f1).
                into(viewHolder.imageView);
        viewHolder.button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                list.remove(position);
                notifyDataSetChanged();
            }
        });
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(context, ViewTwareActivity.class);
                intent.putExtra("pdfattach",list.get(position).getPdfattach());//
                intent.putExtra("name",list.get(position).getName());
                context.startActivity(intent);

            }
        });
    }
    @Override
    public int getItemCount() {
        if (list == null)
            return 0;
        return list.size();
    }
}
